﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using WHC.Pager.Entity;

using WHC.Hotel.BLL;
using WHC.Hotel.Entity;
using WHC.OrderWater.Commons;

namespace WHC.Hotel.UI
{
    public partial class FrmLogHistroy : BaseForm
    {
        public FrmLogHistroy()
        {
            InitializeComponent();
        }
        public override void RefreshForm()
        {
            BindData();
        }
        private void FrmOrder_Load(object sender, EventArgs e)
        {
            EnableDate(false);
            BindData();

            this.winGridViewPager1.ProgressBar = this.toolStripProgressBar1.ProgressBar;
            this.winGridViewPager1.OnPageChanged += new EventHandler(winGridViewPager1_OnPageChanged);
            this.winGridViewPager1.OnStartExport += new EventHandler(winGridViewPager1_OnStartExport);

            if (Portal.gc.HasFunction("LoginHistory/Delete"))
            {
                this.winGridViewPager1.OnDeleteSelected += new EventHandler(winGridViewPager1_OnDeleteSelected);
            }
            this.winGridViewPager1.OnRefresh += new EventHandler(winGridViewPager1_OnRefresh);
            this.winGridViewPager1.AppendedMenu = this.contextMenuStrip1;
        }

        private void winGridViewPager1_OnRefresh(object sender, EventArgs e)
        {
            BindData();
        }

        private void winGridViewPager1_OnDeleteSelected(object sender, EventArgs e)
        {
            if (MessageUtil.ShowYesNoAndTips("您确定删除选定的记录么？") == DialogResult.No)
            {
                return;
            }

            DataGridView grid = sender as DataGridView;
            if (grid != null)
            {
                foreach (DataGridViewRow row in grid.SelectedRows)
                {
                    BLLFactory<LoginLog>.Instance.Delete(row.Cells["ID"].Value.ToString());
                }
                BindData();
            }
        }

         private void winGridViewPager1_OnStartExport(object sender, EventArgs e)
        {
            PagerInfo pagerInfo = new PagerInfo();
            pagerInfo.CurrenetPageIndex = 1;
            pagerInfo.PageSize = int.MaxValue;
            this.winGridViewPager1.AllToExport = BLLFactory<LoginLog>.Instance.GetAllToDataSet(pagerInfo).Tables[0];//product.GetAllToDataSet(pagerInfo).Tables[0];
        }

        private void winGridViewPager1_OnPageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        public void BindData()
        {
            #region 添加别名解析
            this.winGridViewPager1.AddColumnAlias("ID", "编号");
            this.winGridViewPager1.AddColumnAlias("User_ID", "登录用户的ID");
            this.winGridViewPager1.AddColumnAlias("LoginName", "登录名");
            this.winGridViewPager1.AddColumnAlias("RealName", "真实名称");
            this.winGridViewPager1.AddColumnAlias("Note", "日志信息");
            this.winGridViewPager1.AddColumnAlias("Shop_ID", "分店ID");
            this.winGridViewPager1.AddColumnAlias("LastUpdated", "记录日期");
            #endregion

            SearchCondition condition = new SearchCondition();
            condition.AddCondition("LoginName", this.txtLoginName.Text, SqlOperator.Like)
                .AddCondition("RealName", this.txtRealName.Text, SqlOperator.Like)
                .AddCondition("Note", this.txtNote.Text, SqlOperator.Like);

            if (chkUseDate.Checked)
            {
                condition.AddCondition("LastUpdated", dateTimePicker1.Value.ToString("yyyy-MM-dd"), SqlOperator.MoreThanOrEqual, true)
                    .AddCondition("LastUpdated", dateTimePicker2.Value.AddDays(1).ToString("yyyy-MM-dd"), SqlOperator.LessThanOrEqual, true);

            }

            string where = condition.BuildConditionSql(DatabaseType.SqlServer).Replace("Where", "");
            List<LoginLogInfo> list = BLLFactory<LoginLog>.Instance.Find(where, this.winGridViewPager1.PagerInfo);
            this.winGridViewPager1.DataSource = new WHC.Pager.WinControl.SortableBindingList<LoginLogInfo>(list);
        }

        private void EnableDate(bool enable)
        {
            this.dateTimePicker1.Enabled = enable;
            this.dateTimePicker2.Enabled = enable;
        }

        private void chkUseDate_CheckedChanged(object sender, EventArgs e)
        {
            EnableDate(this.chkUseDate.Checked);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        private void SearchControl_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSearch_Click(null, null);
            }
        }

        private void btnDeleteMonthLog_Click(object sender, EventArgs e)
        {
            try
            {
                BLLFactory<LoginLog>.Instance.DeleteMonthLog();
                BindData();
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                MessageUtil.ShowError(ex.Message);
            }
        }
    }
}
